var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loveData: {
      love: 0,
      ratio: 0,
      least: 0,
      value: 0
    },
    love_nums: "",
    integral: "消费积分",
    loveName: "爱心值",
    name: "",
    point: "积分"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.name) {
      this.setData({
        name: options.name
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getdata();
    this.getyz_basic_info();
  },
  loading(message) {
    wx.showToast({
      title: message,
      icon: 'loading'
    });
  },
  getyz_basic_info() {
    try {
      let basic_info = wx.getStorageSync("yz_basic_info");
      if (basic_info) {
        let integral = basic_info.integral;
        this.setData({
          integral,
          loveName: basic_info.love,
          point: basic_info.lang.member_center.credit1
        });
        if (this.data.name == 'toFreeze') return
        if (this.data.name == 'point') {
          wx.setNavigationBarTitle({
            title: `转换${this.data.point}`,
          })
        } else if (this.data.name == 'consumption_dividend_toPoint') {
          wx.setNavigationBarTitle({
            title: `转换${this.data.point}`
          })
        } else {
          wx.setNavigationBarTitle({
            title: `转换${basic_info.integral}`
          })
        }
      }
    } catch (error) {

    }
  },
  getdata() {
    this.loading('获取数据中...');
    let json = {};
    let url = 'plugin.love.Frontend.Modules.Love.Controllers.change-integral.index';
    if (this.data.name == 'point') {
      url = "plugin.love.Frontend.Modules.Love.Controllers.transformation-point.index"
    } else if (this.data.name == 'consumption_dividend_toPoint') {
      url = "plugin.consume-commission.frontend.exchange.index";
      json.pool_id = this.options.pool_id;
    } else if (this.data.name == 'toFreeze') {
      url = "plugin.love.Frontend.Modules.Love.Controllers.usable-love.transform-freeze";
    }
    let urlStr = app.getNetAddresss(url);
    app._postNetWork({
      url: urlStr,
      data: json,
      success: ({
        data: {
          result,
          msg,
          data
        }
      }) => {
        wx.hideToast();
        if (this.data.name == 'toFreeze') {
          wx.setNavigationBarTitle({
            title: `转换${data.freeze_title}`,
          })
        }
        if (result == 1) this.setData({
          loveData: data
        });
        else this.$toast(msg);
      }
    })
  },
  $toast(message) {
    wx.showToast({
      title: message,
      icon: "none"
    })
  },
  confirm() {
    let {
      love_nums,
      loveData
    } = this.data;
    if (love_nums <= 0 || love_nums === "") {
      this.$toast("转化的数量不能为0，也不能为空");
      return false
    } else if ((love_nums) * 1 > loveData.love * 1) {
      this.$toast("转化的数量不能大于总数量");
      return false
    } else if (love_nums * 1 > (loveData.love - loveData.least) * 1) {
      this.$toast("最低保留" + loveData.least + "爱心值")
      return false
    }
    let json = {
      love_nums: this.data.love_nums
    }
    this.loading("正在转化中...");
    let url = "plugin.love.Frontend.Modules.Love.Controllers.change-integral.change";
    if (this.data.name == 'point') {
      url = "plugin.love.Frontend.Modules.Love.Controllers.transformation-point.change";
    } else if (this.data.name == "consumption_dividend_toPoint") {
      url = "plugin.consume-commission.frontend.exchange.exchange";
      json = {};
      json.pool_id = this.options.pool_id;
      json.exchange_amount = this.data.love_nums;
    } else if (this.data.name == "toFreeze") {
      url = "plugin.love.Frontend.Modules.Love.Controllers.usable-love.submit-freeze";
      json.exchange_value = this.data.love_nums;
    }
    let urlStr = app.getNetAddresss(url);
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res)
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500,
            success: () => {
              setTimeout(() => {
                this.getdata();
              }, 500);
            }
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  _bindinput({
    detail: {
      value
    }
  }) {
    this.setData({
      love_nums: value
    })
  },
  allLove() {
    let {
      love,
      least
    } = this.data.loveData;
    if (this.data.name == "toFreeze") {
      this.setData({
        love_nums: this.data.loveData.usable_love
      })
      return
    }
    if (this.data.name == 'point') {
      this.setData({
        love_nums: love
      });
      return
    }
    if (love <= least) {
      this.$toast("当前没有可转换的" + this.data.loveName);
      return false;
    }
    this.setData({
      love_nums: love - least
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})